import dayjs from 'dayjs'

// 1.传入GMT时间，返回距离当前时间的秒数
export function gmtToSeconds(gmtTime: string) {
  const nowTime = dayjs().unix()
  const checkedTime = dayjs(gmtTime).unix()
  return checkedTime - nowTime
}

export function formatTime(time: string) {
  return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}

// 2.将秒数转换为天时分秒格式
export function secondsToDay(seconds: number) {
  const day = Math.floor(seconds / (3600 * 24))
  const hour = Math.floor((seconds % (3600 * 24)) / 3600)
  const minute = Math.floor((seconds % 3600) / 60)
  const second = Math.floor(seconds % 60)

  if (day > 0) {
    return `${day}天${hour}时${minute}分${second}秒`
  } else if (hour > 0) {
    return `${hour}时${minute}分${second}秒`
  } else if (minute > 0) {
    return `${minute}分${second}秒`
  } else return `${second}秒`
}
